package com.liu.my_blog_system.controller.user;


import com.baomidou.mybatisplus.extension.api.ApiController;
import com.liu.my_blog_system.entity.TbUser;
import com.liu.my_blog_system.service.TbUserService;
import com.liu.my_blog_system.utils.Result;
import com.liu.my_blog_system.vo.LoginUserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.Map;

/**
 * tb_user (TbUser)表控制层
 *
 * @author liuchengjie
 * @since 2021-04-07 14:19:04
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController extends ApiController {
    /**
     * 服务对象
     */
    @Autowired
    private TbUserService tbUserService;

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ApiOperation(value = "根据id查询用户")
    public Result findUserById(@PathVariable String id) {
        TbUser user = tbUserService.getById(id);
        if (user == null) {
            return Result.error().message("该用户不存在");
        }
        user.setPassword(null);
        return Result.ok().data("userInfo", user);
    }

    /**
     * 获取用户列表
     * @param current
     * @param size
     * @return
     */
    @ApiOperation(value = "获取用户列表")
    @GetMapping("/findUserList")
    public Result findUserList(@RequestParam("current") int current,
                               @RequestParam("size") int size,
                               HttpServletRequest request,
                               HttpServletResponse response) {
        Result result = tbUserService.findUserList(current,size,request,response);
        return result;
    }
    /**
     * 注册管理员用户
     * @param user 实体对象
     * @return 新增结果
     */
    @ApiOperation(value = "注册管理员用户")
    @PostMapping("/admin_account")
    public Result insert(@RequestBody @Validated TbUser user, HttpServletRequest request) {

        boolean save = tbUserService.saveAdmin(user,request);
        if (save) {
            return Result.ok().message("保存用户成功");
        }
        return Result.ok().message("保存用户失败");
    }

    /**
     * 更新用户信息
     * @param user
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "更新用户数据")
    @PutMapping("/update")
    public Result update(@RequestBody TbUser user,HttpServletRequest request,HttpServletResponse response) {
        Result result = tbUserService.updateUser(user,request,response);
        return result;
    }

    /**
     * 删除用户
     * @param id 主键结合
     * @return 删除结果
     */
//    @PreAuthorize("@permission.admin()")
    @DeleteMapping("delete")
    @ApiOperation("删除用户")
    public Result delete(@RequestParam("user_id") String id,HttpServletRequest request,HttpServletResponse response) {
        Result result = tbUserService.deleteUserById(id, request, response);
        return result;
    }

    /**
     * 用户注册
     * @param user
     * @param emailCode
     * @param captchaCode
     * @param captchaKey
     * @param request
     * @return
     */
    @ApiOperation(value = "普通用户注册")
    @PostMapping("/register")
    private Result register(@RequestBody TbUser user,
                            @RequestParam("emailCode") String emailCode,
                            @RequestParam("captchaCode") String captchaCode,
                            @RequestParam("captchaKey") String captchaKey,
                            HttpServletRequest request) {

        Result result = tbUserService.register(user,emailCode,captchaCode,captchaKey,request);
        return result;
    }

    /**
     * 用户登录
     * @param userVo
     * @param request
     * @param response
     * @return
     */
    @ApiOperation(value = "用户登陆")
    @PostMapping("/login")
    private Result login(@RequestBody LoginUserVo userVo,HttpServletRequest request,HttpServletResponse response) {

        Result result = tbUserService.login(userVo,request,response);
        return result;
    }

    /**
     * 获取图灵验证码
     * @param response
     * @param captchaKey
     */
    @ApiOperation(value = "获取图灵验证码")
    @GetMapping("/Captcha")
    private void getCaptcha(HttpServletResponse response, @RequestParam("captcha_key") String captchaKey) {
        tbUserService.createCaptcha(response, captchaKey);
    }

    /**
     * 邮件发送
     * @param emailAddr
     * @param request
     * @return
     */
    @ApiOperation(value = "发送邮件验证码")
    @GetMapping("/sendEmail")
    private Result sendEmail(@RequestParam("emailAddr") String emailAddr,HttpServletRequest request){
        tbUserService.sendEmail(emailAddr, request);
        return Result.ok();
    }

    /**
     * 检查邮箱
     * @param email
     * @return
     */
    @ApiOperation(value = "邮箱检查")
    @GetMapping("/check_email")
    public Result checkEmail(@RequestParam("email") String email){
        Result result = tbUserService.checkEmail(email);
        return result;
    }

    /**
     * 检查邮箱
     * @param username
     * @return
     */
    @ApiOperation(value = "用户名检查")
    @GetMapping("/check_username")
    public Result checkUsername(@RequestParam("username") String username){
        Result result = tbUserService.checkUsername(username);
        return result;
    }

    /**
     * 修改密码，找回密码
     * @return
     */
    @ApiOperation(value = "修改密码")
    @PutMapping("/update_password")
    public Result updatePassword(@RequestParam("verify_code") @NotBlank(message = "验证码不能为空") String code,
                                 @RequestParam("email") @NotBlank(message = "邮箱不能为空") String email,
                                 @RequestParam("password")@NotBlank(message = "密码不能为空") String password) {
        Result result = tbUserService.updateUserPassword(code,email,password);
        return result;
    }

    @ApiOperation(value = "修改邮箱")
    @PutMapping("/update_email")
    public Result updateEmail(HttpServletRequest request,
                              HttpServletResponse response,
                              @RequestParam("email") @NotBlank(message = "邮箱不能为空") String email,
                              @RequestParam("verify_code") @NotBlank(message = "验证码不能为空") String code) {
        Result result = tbUserService.updateEmail(request,response,email,code);
        return result;
    }

    /**
     * 退出登录
     * @return
     */
    @ApiOperation(value = "退出登录")
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request,HttpServletResponse response) {
        Result result = tbUserService.logout(request,response);
        return result;
    }

    @ApiOperation(value = "测试接口")
    @GetMapping("/test")
    public Result test(HttpServletRequest request,HttpServletResponse response){
        Map user = tbUserService.checkUserStatus(request, response);
        return Result.ok().data("用户信息",user);
    }
}

















